package com.bitrate.dify.controller;

import com.bitrate.dify.client.DifyClient;
import com.bitrate.dify.request.ChatCompletionRequest;
import com.bitrate.dify.response.ChatCompletionResponse;
import com.bitrate.dify.sse.SseEventSourceListener;
import com.bitrate.dify.wx.cp.WxCpConfiguration;
import me.chanjar.weixin.cp.api.WxCpService;
import me.chanjar.weixin.cp.api.impl.WxCpMsgAuditServiceImpl;
import me.chanjar.weixin.mp.api.WxMpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;

@Controller
@RequestMapping("/dify")
public class DifyMsgController {

    @Autowired
    private DifyClient difyClient;

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    @Autowired
    private WxMpService wxMpService;

    @ResponseBody
    @GetMapping("/chat/sse/stream")
    public String sseStreamChat(@RequestParam String message) {
        SseEmitter sseEmitter = new SseEmitter();
        ChatCompletionRequest request = new ChatCompletionRequest();
        request.setQuery(message);
        request.setUser("liuxiaolong");
        difyClient.streamChatCompletions(request, new SseEventSourceListener(sseEmitter, redisTemplate, wxMpService));
        return "success";
    }

    @ResponseBody
    @GetMapping("/chat/sse/block")
    public ChatCompletionResponse sseBlockChat(@RequestParam String message) {
        ChatCompletionRequest request = new ChatCompletionRequest();
        request.setQuery(message);
        request.setUser("liuxiaolong");
        Integer agentId = 1;
        final WxCpService wxCpService = WxCpConfiguration.getCpService(agentId);
        WxCpMsgAuditServiceImpl wxCpMsgAuditService = new WxCpMsgAuditServiceImpl(wxCpService);
        wxCpMsgAuditService.decryptChatData()
        return difyClient.blockChatCompletions(request);
    }
}
